﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kairos.Models;

namespace Kairos.Controllers
{
    public class DocController : Controller
    {
        private myContext db = new myContext();

        //
        // GET: /Doc/Alarme

        [AccessControl(Roles.All)]
        public ActionResult Alarme()
        {
            var documents = db.Documents.Where(d => DateTime.Compare(DateTime.Today, d.DateExpiration) >= 0);

            return View(documents.ToList());
        }

        //
        // POST: /Doc/Alarme

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator|Roles.Worker)]
        public ActionResult Alarme(int id)
        {
            Document document = db.Documents.Find(id);
            if (document == null)
            {
                return HttpNotFound();
            }

            document.DateExpiration = DateTime.Now.AddDays(Math.Min(
                db.MetaDatas1.Find(document.MetaData_1ID).Expiration,
                db.MetaDatas2.Find(document.MetaData_2ID).Expiration));

            db.Entry(document).State = EntityState.Modified;
            db.SaveChanges();

            var documents = db.Documents.Where(d => DateTime.Compare(DateTime.Today, d.DateExpiration) >= 0);

            return View(documents.ToList());
        }

        //
        // GET: /Doc/Details/5

        [AccessControl(Roles.All)]
        public ActionResult Details(int id = 0)
        {
            Document document = db.Documents.Find(id);

            if (document == null)
            {
                return HttpNotFound();
            }

            document.Contenu = RichTextExtensions.RichText(document.Contenu, Request.QueryString);

            return View(document);
        }

        //
        // GET: /Doc/Create

        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Create()
        {
            ViewBag.MetaData1 = new SelectList(db.MetaDatas1, "ID", "Nom");
            ViewBag.MetaData2 = new SelectList(db.MetaDatas2, "ID", "Nom");

            return View();
        }

        //
        // POST: /Doc/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        // TODO: Trouver un moyen de repasser le tag ValidationInput à true 
        [ValidateInput(false)] // CkEditor
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Create(Document document)
        {
            if (ModelState.IsValid)
            {
                document.DateCreation = DateTime.Now;
                document.DateModification = DateTime.Now;

                document.DateExpiration = DateTime.Now.AddDays(Math.Min(
                    db.MetaDatas1.Find(document.MetaData_1ID).Expiration,
                    db.MetaDatas2.Find(document.MetaData_2ID).Expiration));

                document.UserCreation = User.Identity.Name;
                document.UserModification = User.Identity.Name;

                document.Revision = 1;

                db.Documents.Add(document);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = document.ID });
            }

            ViewBag.MetaData1 = new SelectList(db.MetaDatas1, "ID", "Nom", document.MetaData_1ID);
            ViewBag.MetaData2 = new SelectList(db.MetaDatas2, "ID", "Nom", document.MetaData_2ID);

            return View(document);
        }

        //
        // GET: /Doc/Edit/5

        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Edit(int id = 0)
        {
            Document document = db.Documents.Find(id);
            if (document == null)
            {
                return HttpNotFound();
            }

            ViewBag.MetaData1 = new SelectList(db.MetaDatas1, "ID", "Nom", document.MetaData_1ID);
            ViewBag.MetaData2 = new SelectList(db.MetaDatas2, "ID", "Nom", document.MetaData_2ID);

            return View(document);
        }

        //
        // POST: /Doc/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        // TODO: Trouver un moyen de repasser le tag ValidationInput à true 
        [ValidateInput(false)] // CkEditor
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Edit(Document document)
        {
            if (ModelState.IsValid)
            {
                document.DateModification = DateTime.Now;

                document.DateExpiration = DateTime.Now.AddDays(Math.Min(
                    db.MetaDatas1.Find(document.MetaData_1ID).Expiration,
                    db.MetaDatas2.Find(document.MetaData_2ID).Expiration));

                document.UserModification = User.Identity.Name;

                document.Revision += 1;

                db.Entry(document).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Details", new { id = document.ID });
            }
            return View(document);
        }

        //
        // POST: /Doc/Delete/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AccessControl(Roles.Administrator | Roles.Worker)]
        public ActionResult Delete(int id)
        {
            Document document = db.Documents.Find(id);
            db.Documents.Remove(document);
            db.SaveChanges();
            return RedirectToAction("Document", "Search");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}